package com.wh.hotel.service.strategy.time;

import com.wh.hotel.po.ValidTimePo;
import com.wh.hotel.util.DateWhUtil;
import com.wh.hotel.vo.HotelPriceStrategyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author luweije
 */
@Slf4j
@Component
public class AbsoluteValidTime extends ValidTimeAbstract {

    @Override
    public boolean isHit(Date cur, HotelPriceStrategyVo strategyVo) {
        ValidTimePo validTimePo = strategyVo.getValidTimePo();
        return DateWhUtil.isIn(cur, validTimePo.getStartTime(), validTimePo.getEndTime());
    }

    @Override
    public List<Date> calculateValidTime(HotelPriceStrategyVo strategyVo, Date startDt, Date endDt) {
        ValidTimePo validTimePo = strategyVo.getValidTimePo();
        Date left = DateWhUtil.max(startDt, validTimePo.getStartTime());
        Date right;
        if (validTimePo.getEndTime() == null) {
            right = endDt;
        } else {
            right = DateWhUtil.min(endDt, DateWhUtil.addDays(validTimePo.getEndTime(), -1));
        }
        return DateWhUtil.createDateByDay(left, right);
    }
}
